﻿// CCustomHeader.cpp: 实现文件
//

#include "pch.h"
#include "Draw.h"
#include "CCustomHeader.h"


// CCustomHeader

IMPLEMENT_DYNAMIC(CCustomHeader, CHeaderCtrl)

CCustomHeader::CCustomHeader()
{

}

CCustomHeader::~CCustomHeader()
{
}


BEGIN_MESSAGE_MAP(CCustomHeader, CHeaderCtrl)
    ON_MESSAGE(HDM_LAYOUT, OnLayout)
END_MESSAGE_MAP()

// 虚函数
void CCustomHeader::DrawItem(LPDRAWITEMSTRUCT lp)
{
    CDC dc;
    dc.Attach(lp->hDC);

    // 1. 绘制背景
    CRect rect = lp->rcItem;
    if (lp->itemState & ODS_SELECTED) {
        dc.FillSolidRect(rect, RGB(0, 240, 240));
    }
    else {
        dc.FillSolidRect(rect, RGB(240, 240, 240));  // 浅灰色背景
    }
    
    HDITEM hdi{};
    TCHAR szText[256] = { 0 };
    hdi.mask = HDI_TEXT;
    hdi.pszText = szText;
    hdi.cchTextMax = 256;
    // int count = GetItemCount();
    GetItem(lp->itemID, &hdi);
    dc.DrawText(szText, rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);

    // TRACE(_T("rect = %d %d %d %d \n"), rect.left, rect.top, rect.right, rect.bottom);

    dc.Detach();
}

LRESULT CCustomHeader::OnLayout(WPARAM, LPARAM lParam)
{
    LRESULT lResult = CHeaderCtrl::DefWindowProc(HDM_LAYOUT, 0, lParam);
    HD_LAYOUT* pLayout = (HD_LAYOUT*)lParam;
    pLayout->pwpos->cy = 60;  // 强制高度 30 像素
    pLayout->prc->top = 60;   // 调整布局区域
    return lResult;
}

